package com.fbinggun.fbinggunutil.fgive;

import com.comphenix.protocol.utility.StreamSerializer;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

public class Util {

    Fgive fgive;

    public Util(Fgive fgive) {
        this.fgive = fgive;
    }



    protected  void saveItem(ItemStack itemStack,String name) throws IOException {
        try {
            fgive.getFgiveItemSQL().save(name,itemStack);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }
    protected  ItemStack getItemStack(String name){
        ItemStack item = null;
        try {
            item = fgive.getFgiveItemSQL().getItem(name);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        if(item==null){
            return null;
        }
        return item;
    }
    protected  ItemStack getItemStack(String name,int count){
       ItemStack item = getItemStack(name);
       if(item==null){
           return null;
       }
       item.setAmount(count);
        return item;
    }


    public  ItemStack toItemStack(String data)
    {
        StreamSerializer ss=new StreamSerializer();
        try
        {
            return ss.deserializeItemStack(data);
        }catch(Exception e){e.printStackTrace();}
        return null;
    }

    public  String toData(ItemStack item)
    {
        StreamSerializer ss=new StreamSerializer();
        try
        {
            return ss.serializeItemStack(item);
        }catch(Exception e){e.printStackTrace();}
        return null;
    }
}
